<?
    include "../common.php" ;
    $opt = $_POST['opt'] ; 
    $arr = [] ; 
    if( $opt == 'getProDetail' ){
        $pid = $_POST['pid'] ; 
        $sql = "SELECT * FROM `products` WHERE id = {$pid}" ;
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $arr = $one ; 
        }
    }
    if( $opt == 'addCart' ){
        $uid = $_POST['uid'] ;
        $pid = $_POST['pid'] ;
        $pnum = $_POST['pnum'] ;
        $sql1 = "SELECT * FROM `shopcar` WHERE `uid` = {$uid} AND pid = {$pid}";
        $res1 = mysqli_query($link,$sql1);
        if( $one = mysqli_fetch_assoc($res1) ){
            $count = $one['pnum'] + $pnum ; 
            $sql = "UPDATE `shopcar` SET pnum = {$count} WHERE id = {$one['id']}" ;
            $res = mysqli_query($link,$sql);
            if( $res ){
                $arr['status'] = 1 ;
                $arr['info'] = '修改成功!' ;
            } else {
                $arr['status'] = 0 ; 
                $arr['info'] = '操作失败!' ;
            }
        } else {
            $sql = "INSERT INTO `shopcar`(`uid`, `pid`, `pnum`) VALUES ({$uid},{$pid},{$pnum})" ;
            $res = mysqli_query($link,$sql);
            if( $res ){
                $arr['status'] = 1 ;
                $arr['info'] = '添加成功!' ;
            } else {
                $arr['status'] = 0 ; 
                $arr['info'] = '操作失败!' ;
            }
        }
        
    }
    echo json_encode($arr);
?>